package com.bbx.taxi.client.Task;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.AsyncTask;

import com.bbx.androidapi.util.ToastUtil;
import com.bbx.api.sdk.model.official.passanger.Officail_VclassBuild;
import com.bbx.api.sdk.model.official.passanger.Return.Officail_Vehicle;
import com.bbx.api.sdk.model.official.passanger.Return.Officail_Vehicle.VehicleList;
import com.bbx.api.sdk.net.base.JsonBuild;
import com.bbx.api.sdk.net.official.passanger.Officail_VclassNet;
import com.bbx.api.sdk.net.official.passanger.Officail_VehicleNet;
import com.bbx.taxi.client.Bean.Attribute.MainAttribute;
import com.bbx.taxi.client.Task.BaseAsyncTask;
import com.bbx.taxi.client.MyApplication;
import com.bbx.taxi.official.R;

/** 
 * @Path com.bbx.taxi.client.Task.MyVclassTask.java
 * @Author 严旭文
 * @Date 2016-1-22 上午10:38:26
 * @Company 蓝海（福建）信息技术有限公司
 * @Description
 * 车型列表
 */
public class MyVclassTask {
	private Activity context;
	private MyApplication mApplication;
	public static MyVclassTask instance;
	public VclassTask mVclassTask;
	public interface OnVclassListener {
		/**
		 *车型列表，求完毕返回list为空则失败,
		 *参数：
		 *   0.cb_position为车子顺序从0开始
		 *   1.vtype 车子类型
		 *   2.vtype_index 某个车型的第几项类型,该字段只有automatic为true有用，主要用于字段选择车型后然后在自动选择车型的第几项,automatic为false的时候，传0即可
		 *   3.list 车型列表
		 *   4.automatic true为自动的
		 */
		public void onVclass(int cb_position,int vtype,int vtype_index,ArrayList<VehicleList> list,boolean isautomatic);
	}

	static OnVclassListener mOnVclassListener = null;

	public static void setOnVclassListener(OnVclassListener l) {
		mOnVclassListener = l;
	}
	/**
	 *车型列表，求完毕返回list为空则失败,
	 *参数：
	 *   1.vtype 车子类型
	 *   2.vtype_index 某个车型的第几项类型,该字段只有automatic为true有用，主要用于字段选择车型后然后在自动选择车型的第几项，
	 *   3.list 车型列表
	 *   4.automatic true为自动的
	 */
	public static MyVclassTask getInstance(Activity context,
			MyApplication mApplication,int cb_position,int vtype,int vtype_index,boolean isautomatic) {
		instance = new MyVclassTask(context, mApplication,cb_position,vtype,vtype_index,isautomatic);
		return instance;
	}
	public MyVclassTask(Activity context, MyApplication mApplication,int cb_position,int vtype,int vtype_index,boolean isautomatic) {
		this.context = context;
		this.mApplication = mApplication;
		getVclass(cb_position,vtype,vtype_index,isautomatic);
	}
	/**
	 * 获取车型列表
	 */
	public void getVclass(int cb_position,int vtype,int vtype_index,boolean isautomatic) {
		Officail_VclassBuild mOfficail_VclassBuild = new Officail_VclassBuild(context);
		mOfficail_VclassBuild.passenger_id=mApplication.getUid();
		mOfficail_VclassBuild.vtype=vtype;
		mOfficail_VclassBuild.pos=0;
		mOfficail_VclassBuild.limit=100;
		mVclassTask = new VclassTask(context, mOfficail_VclassBuild,cb_position,vtype_index,isautomatic);
		mVclassTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
	}
	private class VclassTask extends BaseAsyncTask {

		private Officail_VclassBuild mOfficail_VclassBuild;
		private boolean isautomatic;
		private int vtype_index;
		private int cb_position;

		public VclassTask(Activity context, Officail_VclassBuild mOfficail_VclassBuild,int cb_position,int vtype_index,boolean isautomatic) {
			super(context);
			this.mOfficail_VclassBuild = mOfficail_VclassBuild;
			this.isautomatic=isautomatic;
			this.vtype_index=vtype_index;
			this.cb_position=cb_position;
		}

		@Override
		public void onSuccess(Object object) {
			if (object != null) {
				Officail_Vehicle mOfficail_Vehicle = (Officail_Vehicle)object;
				ArrayList<VehicleList> list = mOfficail_Vehicle.getList();
				if(list!=null&&!list.isEmpty()){
				  //根据公费跟自费，过滤掉一下车型不匹配的,
					MainAttribute main=MainAttribute.getInstance();
					for(int i=list.size()-1;i>=0;i--){
						if(!list.get(i).getOtype().equals(main.getOwner_type()+"")){
							list.remove(i);
						}
					}
					
				if (mOnVclassListener != null) {
					mOnVclassListener.onVclass(cb_position,mOfficail_VclassBuild.vtype,vtype_index,list,isautomatic);
				 }
                }else{
    				if (mOnVclassListener != null) {
    					mOnVclassListener.onVclass(cb_position,mOfficail_VclassBuild.vtype,vtype_index,null,isautomatic);
    				}
                }

			} else {
				ToastUtil.showToast(context, R.string.no_network);
				if (mOnVclassListener != null) {
					mOnVclassListener.onVclass(cb_position,mOfficail_VclassBuild.vtype,vtype_index,null,isautomatic);
				}
			}
		}

		@Override
		public void onFailed(int status, String msg) {
			ToastUtil.showToast(context, R.string.no_network);
			if (mOnVclassListener != null) {
				mOnVclassListener.onVclass(cb_position,mOfficail_VclassBuild.vtype,vtype_index,null,isautomatic);
			}
		}

		@Override
		protected Object doInBackground(Object... params) {
			// TODO Auto-generated method stub
			String json = new JsonBuild().setModel(mOfficail_VclassBuild).getJson();
			return new Officail_VclassNet(context, json);
		}
	}

}
